class PartStockoutsController < ApplicationController
  before_action :set_menu

  def index
    @submenu = :part_stockout
    @title = '配件出库单'
    @part_stockouts = PartStockout.all
    if params.has_key? :approve
      @part_stockout = PartStockout.find params[:approve]
      @part_stockout.update(status: 1)
      @part_stockout.minus_inventory
    end
  end

  def new
    @part_stockout = PartStockout.new
  end

  def create
    @part_stockout = PartStockout.new post_params
    if @part_stockout.save
      redirect_to part_stockout_path(:id => @part_stockout.id)
    else
      render :action => 'new'
    end
  end

  def show
    @title = "配件出库单/明细"
    @buttons.push :action => part_stockouts_path, :label => "返回"
    @part_stockout = PartStockout.find params[:id]
  end

  def destroy
    @part_stockout = PartStockout.find params[:id]
    @part_stockout.destroy
    redirect_to part_stockouts_path
  end


  def post_params
    params.require(:part_stockout).permit(*%w(user_id note))
  end

  private
  def set_menu
    @mainmenu = :part
  end
end
